import User from './user';
import Application from './Application';
import ThirdApplication from './ThirdApplication';
import Token from './token';
import Permission from './Permission';
import Role from './Role';
import appService from '@/utils/appService';

const Layout = resolve => {
  require(['./Layout'], resolve);
};

const OrgListView = resolve => {
  require(['./OrgListView'], resolve);
};

const routeConfig = {
  path: '/admin',
  redirect: to => {
    let redirectPath = 'application';
    const routeConfig = window.routeConfig[0];
    const childrens = routeConfig.children;
    for (let i = 0; i < childrens.length; i++) {
      const permission = childrens[i].meta.permission;
      const role = childrens[i].meta.role;
      if (appService.hasRole(role) && appService.hasPermission(permission)) {
        redirectPath = childrens[i].path;
        break;
      }
    }
    if (redirectPath.indexOf('/') !== 0) {
      redirectPath = routeConfig.path + '/' + redirectPath;
    }
    return redirectPath;
  },
  component: Layout,
  meta: {
    title: '管理',
    name: '管理',
    show: false
  },
  children: [...Application, ...ThirdApplication, ...Permission, ...Role, {
    path: 'org',
    component: OrgListView,
    meta: {
      title: '机构',
      name: '机构',
      icon: 'md-contacts',
      permission: 'auth:menu:org:list',
      componentName: 'OrgListView',
      keepAlive: true
    }
  }, ...User, ...Token],
  beforeEnter: (to, from, next) => {
    next();
  }
};

window.routeConfig = [routeConfig];
export default [routeConfig];
